package cn.huang.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import cn.huang.constant.MessageConstant;
import cn.huang.entity.PageResult;
import cn.huang.entity.QueryPageBean;
import cn.huang.entity.Result;
import cn.huang.pojo.Order;
import cn.huang.service.OrderService;
import cn.huang.utils.SMSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderService.pageQuery(queryPageBean);
        return pageResult;

    }


    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmealIds) {

        Result result = null;
        try {
            if (setmealIds != null && setmealIds.length > 0) {
                for (Integer setmealId : setmealIds) {
                    String s = setmealId.toString();
                    map.put("setmealId", s);
                    map.put("orderType", Order.ORDERTYPE_TELEPHONE);
                    result = orderService.order(map);
                }
                return result;

            } else {
                return new Result(false, "请选择体检套餐");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }

    /**
     * 业务逻辑：单个取消
     * 1.确认取消后，将预约状态变为已取消
     * 2.发送短信告知用户已取消预约；
     *
     * @return
     */
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(@RequestBody Map map, Integer id) {
        try {
            //1.确认取消后，将预约状态变为已取消

            orderService.cancelOrder(map, id);
            //发送短信告知用户已取消预约；
            String name = (String) map.get("name");
            String phoneNumber = (String) map.get("phoneNumber");
            String orderDate = (String) map.get("orderDate");
            if (phoneNumber != null && phoneNumber != "") {
                System.out.println(phoneNumber);
                System.out.println("短信已发送！===========");
            }
            System.out.println("==========================");
            return new Result(true, "取消成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "取消失败");
        }

    }

    /**
     * 批量取消业务逻辑
     * 1.前端发送的请求传递的参数为装有order 的json'数据的集合
     * 2.遍历集合后，调用单个取消的方法
     *
     * @param orderList
     * @return
     */
    @RequestMapping("/cancelSelected")
    public Result cancelSelected(@RequestBody List<Map<String, Object>> orderList) {
        System.out.println(orderList);
        try {
            if (orderList != null && orderList.size() > 0) {
                for (Map map : orderList) {

                    Integer id = (Integer) map.get("id");
                    String phoneNumber = (String) map.get("phoneNumber");
                    String orderDate = (String) map.get("orderDate");
                    String name = (String) map.get("name");
                    orderService.cancelOrder(map, id);
                    if (phoneNumber != null && phoneNumber != "") {
                        SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, phoneNumber, orderDate);
                    }
                }
            }
            return new Result(true, "批量取消成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "批量取消失败");
        }

    }


    //确认预约方法
    @RequestMapping("/confirm")
    public Result confirmOrder(@RequestBody Map map) {
        String phoneNumber = (String) map.get("phoneNumber");
        String orderDate = (String) map.get("orderDate");
        String name = (String) map.get("name");
        if (phoneNumber != null && orderDate != null) {
            orderService.confirmOrder(map);
            System.out.println("===============短信已经发送");
            return new Result(true, MessageConstant.CONFIRMORDER_SUCCESS);

        } else {
            return new Result(false, MessageConstant.CONFIRMORDER_FAIL);
        }
    }


}
